/*************************
查看
* */
<template>
  <div>
    <el-dialog
      title="事件考核机构人员分摊"
      :close-on-click-modal="true"
      :visible.sync="visible"
      top="8vh"
      width="70%"
      @close="closeDialog()"
    >
      <el-form ref="dataForm" :model="dataForm" class="boxsty" label-width="100px"
               @keyup.enter.native="dataFormSubmit()">
        <el-form-item label="标题：" prop="sjbt">
          <span>{{ dataForm.sjbt }}</span>
        </el-form-item>

        <el-form-item label="机构名称：" prop="khzq">
          <span>{{ dataForm.realname }}</span>
        </el-form-item>

        <el-form-item label="指标规则：" prop="sjdesc">
          <span>{{ dataForm.zbgz }}</span>
        </el-form-item>

        <el-form-item label="分总值：" prop="sjdesc">

          <span v-if="parseFloat(zfz)==parseFloat(dataForm.zzfz).toFixed(2)">{{ parseFloat(dataForm.zzfz).toFixed(2) }}/{{ zfz }}</span>
          <span v-else style="color:red">{{ parseFloat(dataForm.zzfz).toFixed(2) }}/{{ zfz }}</span>
        </el-form-item>

        <el-form-item label="总奖金：" prop="sjdesc">
          <span v-if="parseFloat(zjj)==parseFloat(dataForm.zzjj).toFixed(2)">{{ parseFloat(dataForm.zzjj).toFixed(2) }}/{{ zjj }}</span>
          <span v-else style="color: red">{{ parseInt(dataForm.zzjj).toFixed(2) }}/{{ zjj }}</span>
        </el-form-item>
        <el-form-item>
          <el-button
            icon="el-icon-search"
            type="primary"
            class="marginLeft"
            @click="yxbtexst"
          >一键均摊
          </el-button>

          <!--          <el-button-->
          <!--            icon="el-icon-search"-->
          <!--            type="primary"-->
          <!--            class="marginLeft"-->
          <!--            @click="addUserList"-->
          <!--          >保存数据-->
          <!--          </el-button>-->

        </el-form-item>
        <el-form-item label="考核对象：">
          <el-table
            ref="multipleTable"
            :data="tableData"
            height="280"
            style="width: 100%;"
            tooltip-effect="dark"
            v-loading="dataListLoading"
          >
            <el-table-column type="index" width="70" align="center" label="序号"></el-table-column>

            <el-table-column
              prop="username"
              label="员工编号" min-width="120" align="center" show-overflow-tooltip>
            </el-table-column>

            <el-table-column
              prop="ryName"
              label="姓名" min-width="120" align="center" show-overflow-tooltip>
            </el-table-column>

            <el-table-column
              fixed="right"
              prop="zzjj"
              label="加扣奖金" align="center" width="100">
              <template slot-scope="scope">
                <el-input v-model="scope.row.zzjj" placeholder="" @blur="addjj()"
                          style="margin-right:10px"></el-input>
              </template>
            </el-table-column>

            <el-table-column
              fixed="right"
              prop="zzfz"
              label="加扣分值" align="center" width="100">
              <template slot-scope="scope">
                <el-input v-model="scope.row.zzfz" @blur="addfz()" placeholder=""
                          style="margin-right:10px"></el-input>
              </template>
            </el-table-column>

          </el-table>
        </el-form-item>


        <el-form-item label="申诉理由：" prop="ssly" v-if="sslyShow">
          <span>{{ dataForm.ssly }}</span>
        </el-form-item>

        <el-form-item label="申诉回复：" prop="sshf" v-if="sshfShow">
          <span>{{ dataForm.sshf }}</span>
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
         <el-button type="primary" plain @click="closeDialog()">返回</el-button>
        <el-button v-if="dataForm.zttype!=2" type="primary" plain @click="addUserList">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {dataDetail, fetchList, fetchUserList, saveList, uptypeByBh} from "@/api/dailyExamine/eventExamine/resultsLook";

export default {
  data() {
    return {
      tableData: [],
      showRY: true,
      zfz: 0,
      zjj: 0,
      dataListLoading: false,
      visible: false,
      dataForm: {},
      sslyShow: false,
      sshfShow: false
    };
  },
  watch: {},
  created() {
  },
  methods: {
    //初始化
    csh() {
      this.dataForm = {};
    },
    init(obj) {
      if (obj) {
        this.sslyShow = obj.ssly == null || obj.ssly == '' ? false : true;
        this.sshfShow = obj.sshf == null || obj.sshf == '' ? false : true;
        this.dataForm = obj;
        this.showRY = obj.khtype == 'ry' ? true : false;
        this.$nextTick(() => {
          this.$refs.multipleTable.doLayout();
        })
        this.dataView(obj);
      }
      this.visible = true;
    },

    addjj() {
      this.zjj = 0;
      let zs = 0;
      this.tableData.forEach(itme => {
        zs += parseFloat(itme.zzjj);
      })
      this.zjj = zs.toFixed(2);
    },

    addfz() {
      this.zfz = 0;
      let zs = 0;
      this.tableData.forEach(itme => {
        zs += parseFloat(itme.zzfz);
      })
      this.zfz = zs.toFixed(2);
    },

    dataView(row) {
      fetchList(
        Object.assign(
          {
            current: 1,
            size: 999,
          },
          {jgid: this.dataForm.id}
        )
      ).then(response => {
        let tableDatas = response.data.data.records;
        if (tableDatas.length > 0) {
          this.tableData = tableDatas;
        } else {
          this.selectNewUser(row);
        }
        this.addjj();
        this.addfz();
      })
    },

    selectNewUser(row) {
      fetchUserList(
        Object.assign(
          {
            current: 1,
            size: 99,
          },
          {deptId: row.deptid}
        )
      ).then(response => {
        let tableDatas = response.data.data.records;
        tableDatas.forEach(itme => {
          itme.zzjj = 0;
          itme.zzfz = 0;
        });
        this.tableData = tableDatas;
      });
    },

    addUserList() {
      this.upJgZtType();
      if (parseInt(this.dataForm.zzfz) < this.zfz) {
        this.$notify.error("分配的分值不能大于总分值")
        return false;
      }
      if (parseInt(this.dataForm.zzjj) < this.zjj) {
        this.$notify.error("分配的奖金不能大于总奖金")
        return false;
      }
      if (parseInt(this.dataForm.zzfz) > this.zfz) {
        this.$notify.error("分值未分配完成")
        return false;
      }

      if (parseInt(this.dataForm.zzjj) > this.zjj) {
        this.$notify.error("奖金未分配完成")
        return false;
      }
      this.tableData.forEach(item => {
        item.jgid = this.dataForm.id;
      });
      saveList(Object.assign(this.tableData)).then(response => {
        this.$notify.success('保存成功');
        this.upJgZtType();
        this.closeDialog();
      });
    },

    upJgZtType(){
      uptypeByBh({bh: this.dataForm.id}).then(response =>{
      });
    },




    yxbtexst() {
      this.zjj = 0;
      this.zfz = 0;
      let tableDatas = this.tableData;
      let jj = Number((this.dataForm.zzjj / this.tableData.length).toFixed(2));
      let fz = Number((this.dataForm.zzfz / this.tableData.length).toFixed(2));
      tableDatas.forEach(itme => {
        itme.zzjj = jj;
        itme.zzfz = fz;
      })
      this.tableData = tableDatas;
      this.addjj();
      this.addfz();

    },
    closeDialog() {
      this.tableData = [];
      this.dataForm = {};
      this.$emit('refreshDataList');
      this.$emit("closefjjj");
    },
    handleChange(value) {
    }
  }
};
</script>
<style scoped>
/deep/ .boxsty {
  padding: 20px 30px !important;
}

/deep/ .el-table th {
  padding: 5px !important;
}

.el-divider {
  background-color: #fff;
}

.tree-box {
  height: 500px;
  overflow: auto;
}
</style>
